<?php

namespace App\Trip\Plane\Requests;

use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class TicketQueryRequest implements Arrayable
{
    use PageRequest;

    private Request $req;
    private mixed   $sort_key;
    private mixed   $cabins;
    private mixed   $plane_types;
    private mixed   $child_types;
    private mixed   $depart_airports;
    private mixed   $arrive_airports;
    private mixed   $companies;
    private mixed   $sort_value;
    private mixed   $depart_time;
    private mixed   $only_direct;
    private mixed   $to;
    private mixed   $from;
    private string  $trip_date;
    private array   $data;

    /**
     * @throws Exception
     */
    public function __construct(Request $request)
    {
        $this->data            = $request->toArray();
        $this->from            = $request->input('from');
        $this->to              = $request->input('to');
        $this->trip_date       = $request->input('trip_date');
        $this->only_direct     = $request->input('only_direct', 1);
        $this->sort_key        = $request->input('sort_key', '');
        $this->depart_time     = $request->input('depart_time', '');
        $this->sort_value      = $request->input('sort_value', '');
        $this->companies       = $request->input('companies', []);
        $this->arrive_airports = $request->input('arrive_airports', []);
        $this->depart_airports = $request->input('depart_airports', []);
        $this->child_types     = $request->input('child_types', []);
        $this->plane_types     = $request->input('plane_types', []);
        $this->cabins          = $request->input('cabins', []);
        if ($this->getFrom()->getCode() == $this->getTo()->getCode()) {
            throw new Exception('param error');
        }
    }

    public function toArray()
    {
        return $this->data;
    }

    public function getFrom(): AirportRequest
    {
        return new AirportRequest($this->from);
    }

    public function getTo(): AirportRequest
    {
        return new AirportRequest($this->to);
    }

    public function getTripDate()
    {
        return $this->trip_date;
    }

    public function getOnlyDirect()
    {
        return $this->only_direct;
    }

    public function getDepartTime()
    {
        return $this->depart_time;
    }

    /**
     * order key
     * @return string
     */
    public function getOrderBy()
    {
        return $this->sort_key;
    }

    /**
     * order value
     * @return string
     */
    public function getSortBy()
    {
        return $this->sort_value;
    }

    public function getCompanies()
    {
        return $this->companies;
    }

    public function getArriveAirports()
    {
        return $this->arrive_airports;
    }

    public function getDepartAirports()
    {
        return $this->depart_airports;
    }

    public function getPlaneTypes()
    {
        return $this->plane_types;
    }

    public function getCabins()
    {
        return $this->cabins;
    }

    public function getChildTypes()
    {
        return $this->child_types;
    }

    public function getRequestParamKey(): string
    {
        $key = sha1(json_encode($this->toArray(), true));
        Cache::put($key, $this, 60 * config('trip.ttl.minutes'));
        return $key;
    }
}